package com.wdc.n1;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.Test17")

public class Test17 {

    public static void main(String[] args) {
        //创建一个共享的锁对象，用于协调两个线程
        Object lock = new Object();

        Thread t1 = new Thread(() -> {

            synchronized (lock) {
                try {
                    log.info("老王洗水壶");
                    Thread.sleep(1000);
                    log.info("老王烧开水");
                    Thread.sleep(5000);
                    log.info("老王烧水完毕，交水壶给小王");


                    //烧水完毕后，通知小王可以开始泡茶
                    lock.notify();  //通知小王
                    lock.wait();  //等待小王泡茶完毕
                    log.info("小王泡茶完毕，到老王这里了");

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }, "老王");

        Thread t2 = new Thread(() -> {

            synchronized (lock) {
                try {
                    log.info("小王洗茶壶");
                    Thread.sleep(1000);
                    log.info("小王洗茶杯");
                    Thread.sleep(1000);
                    log.info("小王拿茶叶");
                    Thread.sleep(1000);

                    log.info("小王拿好茶叶，交给老王");



                    //烧水完毕后，通知小王可以开始泡茶
                    lock.notify();  //通知小王
                    lock.wait();  //等待小王泡茶完毕
                    log.info("小王泡茶完毕，可以开始drink 了");

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }, "小王");

        t1.start();
        t2.start();


    }
}
